<?php


namespace App\Library\Tools;


use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Input\Input;

class Upload
{

    /**
     * 文件上传
     * @param \Illuminate\Http\UploadedFile $fileObj 文件对象
     * @param string $dir 存储目录
     * @return array
     */
    public static function curlFile($fileObj, $dir = 'img')
    {
        header('content-type:text/html;charset=utf8');
        $fileData = file_get_contents($fileObj->getRealPath());

        $file = public_path('static' . DIRECTORY_SEPARATOR . time() . rand(1000, 9999) . '.' . $fileObj->getClientOriginalExtension());
//        $file = './static/' . time() . rand(1000, 9999) . '.' . $fileObj->getClientOriginalExtension();

        $ret = file_put_contents($file, $fileData);
        if ($ret) {
            $upload_key = config('server.file_upload_key');
            $fileBaseName = basename($file);
            $key = md5($fileBaseName . $dir . $upload_key);
            $upload_url = config('server.file_upload_address');
            $img_url = config('server.file_upload_upload_url');
            $params = [
                'file' => new \CurlFile(realpath($file)),
                'type' => $dir,
                'key' => $key,
            ];
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $upload_url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

            $ret = curl_exec($ch);
            curl_close($ch);
            unlink($file);
            $rest = json_decode(trim($ret, chr(239) . chr(187) . chr(191)), true);
            if (empty($rest) || !isset($rest['code']) || $rest['code'] != 0) {
                Log::info(__METHOD__ . ' http curl response : ' . $ret);
                return ['code' => $rest['code'], 'msg' => '上传失败' . $rest['msg']];
            }

            $data = [
                'code' => 0,
                'msg' => '上传成功',
                'newfile' => $rest['newfile'],
                'path' => $dir . '/' . $rest['newfile'],
                'url' => $upload_url . $rest['url'],
                'img' => $img_url . '/' . $dir . '/' . $rest['newfile'],
            ];
            return $data;
        }
        return ['code' => 9, 'msg' => '写入文件失败'];
    }



    public static function curl($url,$params){
        header('Content-Type:application/x-www-form-urlencoded;charset=utf8');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        $ret = curl_exec($ch);
        curl_close($ch);
        $rest = json_decode(trim($ret, chr(239) . chr(187) . chr(191)), true);
        return $rest;
    }


}
